from app import db
from blueprints import api_blueprint
from tools.decorate import pack_session
from tools.image import ImageDefault
from tools.model import QueryOneInstance, UpdateInstance
from tools.response import  Response, ResponsePostJson

class PersonalInformation(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    intro = db.Column(db.String(120), default='该用户暂无介绍')
    uid = db.Column(db.Integer, nullable=False)
    role = db.Column(db.String(50), default='访客')
    img_icon = db.Column(db.Text, default=lambda :ImageDefault())
    img_background = db.Column(db.Text, default=lambda :ImageDefault())

@api_blueprint.route('/PersonalInformation/intro', methods=['POST'])
@pack_session
def update_personal_intro(data):
    def cb(json):
        UpdateInstance(QueryOneInstance(PersonalInformation, {'uid': data['session']['uid']}), json)
        return Response("个人介绍更新成功")
    return ResponsePostJson(['intro'], cb)

@api_blueprint.route('/personal/PersonalInformation/role', methods=['POST'])
@pack_session
def update_personal_role(data):
    def cb(json):
        UpdateInstance(QueryOneInstance(PersonalInformation, {'uid': data['session']['uid']}), json)
        return Response("角色信息更新成功")
    return ResponsePostJson(['role'], cb)

@api_blueprint.route('/personal/PersonalInformation/img_icon', methods=['POST'])
@pack_session
def update_personal_img_icon(data):
    def cb(json):
        UpdateInstance(QueryOneInstance(PersonalInformation, {'uid': data['session']['uid']}), json)
        return Response("头像更新成功")
    return ResponsePostJson(['img_icon'], cb)

@api_blueprint.route('/personal/PersonalInformation/img_background', methods=['POST'])
@pack_session
def update_personal_img_background(data):
    def cb(json):
        UpdateInstance(QueryOneInstance(PersonalInformation, {'uid': data['session']['uid']}), json)
        return Response("背景图片更新成功")
    return ResponsePostJson(['img_background'], cb)